package com.sdruixinggroup.mondayb2b.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.models.Picture;

/**
 * Created by 00029935 on 2017/6/22.
 */

public class SelectPicAdapter extends QuickAdapter<Picture> {
    private Context context;
    private SelectPicListener selectPicListener;
    private RemoveItemListener removeItemListener;
    public SelectPicAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getLayoutID(int viewType) {
        return R.layout.select_pic_adapter_item;
    }

    @Override
    public void convert(final VH holder, final Picture itemData, final int position) {
        ImageView imageView = holder.getView(R.id.add_pic_one);
      final   String url = itemData.getUrl();

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(null != selectPicListener && TextUtils.isEmpty(url)){
                    selectPicListener.listener(itemData,position);
                }
            }
        });

        Glide.with(context).load(url).centerCrop().crossFade().placeholder(R.mipmap.add_photo).error(R.mipmap.add_photo).into(imageView);
        ImageView clear = holder.getView(R.id.close);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getListData().remove(position);
                notifyDataSetChanged();
                if(null != removeItemListener){
                    removeItemListener.listener(itemData,position);
                }

            }
        });

        if(TextUtils.isEmpty(url)){
            clear.setVisibility(View.INVISIBLE);
        }else {
            clear.setVisibility(View.VISIBLE);
        }

    }

    public interface SelectPicListener{
        void listener(Picture itemData, int position);
    }

    public void setSelectPicListener(SelectPicListener selectPicListener) {
        this.selectPicListener = selectPicListener;
    }

    public interface  RemoveItemListener{
        void listener(Picture itemData, int position);
    }

    public void setRemoveItemListener(RemoveItemListener removeItemListener) {
        this.removeItemListener = removeItemListener;
    }
}
